﻿Imports Aricie.DNN.UI.Controls
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Services.Exceptions.Exceptions
Imports SD.LLBLGen.Pro.ORMSupportClasses
Imports Aricie.DNN.Services.Url.FriendlyUrl.Data
Imports Aricie.DNN.Services.Url.FriendlyUrl.EntityClasses
Imports Aricie.DNN.Services.Url.FriendlyUrl.HelperClasses
Imports Aricie.DNN.Security.Trial
Imports Aricie.DNN.Services.Url.FriendlyUrl.Security

Namespace Aricie.DNN.Services.Url.FriendlyUrl

    Partial Public Class FriendlierUrlProviderStoreEdit
        Inherits AriciePortalModuleBase
        Implements IActionable

        Public Const CONTROL_KEY As String = "EditStore"


#Region "Event Handlers"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                Me.dsStore.AdapterToUse = DataProvider.Instance.GetAdapter()
                Me.BindFilterPanel()
                InitFilter()
                Me.EnforceFreeVersion()
            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try
        End Sub

        Private Sub dsStore_PerformGetDbCount(ByVal sender As Object, ByVal e As SD.LLBLGen.Pro.ORMSupportClasses.PerformGetDbCountEventArgs2) Handles dsStore.PerformGetDbCount
            e.DbCount = e.ContainedCollection.Count
        End Sub

        Private Sub dsStore_PerformSelect(ByVal sender As Object, ByVal e As SD.LLBLGen.Pro.ORMSupportClasses.PerformSelectEventArgs2) Handles dsStore.PerformSelect
            Me.dsStore.AdapterToUse.FetchEntityCollection(e.ContainedCollection, e.Filter)
        End Sub

        Private Sub dsStore_PerformWork(ByVal sender As Object, ByVal e As SD.LLBLGen.Pro.ORMSupportClasses.PerformWorkEventArgs2) Handles dsStore.PerformWork
            Try
                For Each element As UnitOfWorkElement2 In e.Uow.GetEntityElementsToInsert
                    Dim fupMappingEntity As FupUrlMappingEntity = DirectCast(element.Entity, FupUrlMappingEntity)
                    fupMappingEntity.CreatedDate = Now
                Next
                For Each element As UnitOfWorkElement2 In e.Uow.GetEntityElementsToUpdate
                    Dim fupMappingEntity As FupUrlMappingEntity = DirectCast(element.Entity, FupUrlMappingEntity)
                    fupMappingEntity.CreatedDate = Now
                Next
                e.Uow.Commit(Me.dsStore.AdapterToUse, True)

            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try

        End Sub

        Protected Sub cmdFilter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdFilter.Click
            Try
                If Me.ctlFilterPanel.IsValid Then


                    Dim filterPanel As FilterPanel = DirectCast(Me.ctlFilterPanel.DataSource, FilterPanel)

                    Me.BindFilterPanel()

                    Me.dsStore.FilterToUse = New RelationPredicateBucket

                    If Not String.IsNullOrEmpty(filterPanel.FriendlyUrlFilter) Then
                        Me.dsStore.FilterToUse.PredicateExpression.AddWithAnd(New FieldLikePredicate(FupUrlMappingFields.FriendlyUrl, Nothing, "%" & filterPanel.FriendlyUrlFilter & "%"))
                    End If

                    If Not String.IsNullOrEmpty(filterPanel.RealUrlFilter) Then
                        Me.dsStore.FilterToUse.PredicateExpression.AddWithAnd(New FieldLikePredicate(FupUrlMappingFields.RealUrl, Nothing, "%" & filterPanel.RealUrlFilter & "%"))
                    End If

                    Me.gvStoreList.DataBind()
                End If
            Catch ex As Exception
                ProcessModuleLoadException(Me, ex)
            End Try
        End Sub


#End Region


#Region "Private Methods"

        Private Sub EnforceFreeVersion()

            Dim tc As TrialController = TrialController.Instance(New TrialProvider())
            Dim objTrialStatus As TrialStatusInfo = tc.LimitModule(Me, True)
            If objTrialStatus.Status = TrialStatus.Expired Then
                Me.gvStoreList.AllowInsert = False
                Me.gvStoreList.Enabled = False
            End If
        End Sub

        Private Sub BindFilterPanel()
            Dim FilterPanelSettings As New FilterPanel
            Me.ctlFilterPanel.LocalResourceFile = Me.SharedResourceFile
            If Me.ctlFilterPanel.DataSource Is Nothing Then
                Me.ctlFilterPanel.DataSource = FilterPanelSettings
            End If
            Me.ctlFilterPanel.DataBind()
        End Sub

        Private Sub InitFilter()
            Dim FriendlyUrlDropDownList As String = ""
            Dim FriendlyUrlFilter As String = ""
            Dim RealUrlFilter As String = ""

            If Not Me.Request.QueryString("FriendlyUrlDropDownList") Is Nothing Then
                FriendlyUrlDropDownList = Me.Request.QueryString("FriendlyUrlDropDownList").ToString
            End If

            If Not Me.Request.QueryString("FriendlyUrlFilter") Is Nothing Then
                FriendlyUrlFilter = Me.Request.QueryString("FriendlyUrlFilter").ToString
            End If

            If Not Me.Request.QueryString("RealUrlFilter") Is Nothing Then
                RealUrlFilter = Me.Request.QueryString("RealUrlFilter").ToString
            End If

        End Sub
#End Region

#Region "IActionable"


        Public ReadOnly Property ModuleActions() As DotNetNuke.Entities.Modules.Actions.ModuleActionCollection _
            Implements DotNetNuke.Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New DotNetNuke.Entities.Modules.Actions.ModuleActionCollection
                Actions.Add(GetNextActionID, _
                            Localization.GetString( _
                            FriendlierUrlProviderSettings.CONTROL_KEY, _
                            SharedResourceFile), _
                             DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, _
                             "", "edit.gif", _
                             DotNetNuke.Common.Globals.NavigateURL(), _
                             False, _
                             DotNetNuke.Security.SecurityAccessLevel.Host, _
                             True, _
                             False)

                Actions.Add(GetNextActionID, _
                            Localization.GetString( _
                            FriendlierUrlProviderConfig.CONTROL_KEY, _
                            SharedResourceFile), _
                             DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, _
                             "", "edit.gif", _
                              Me.EditUrl(FriendlierUrlProviderConfig.CONTROL_KEY), _
                             False, _
                             DotNetNuke.Security.SecurityAccessLevel.View, _
                             True, _
                             False)

                Return Actions
            End Get
        End Property


#End Region
    End Class

End Namespace

